<template>
	<div>
		<!-- 出库材料信息 -->
		<div style="width: 100%;height: 15vh;background-color: #4142F4">
			<div style="color: #FFFFFF;font-weight: 700;font-size: 38rpx;margin-left: 10%;padding-top: 6%">
				出库
			</div>
		</div>
		<!-- 出库详细信息 -->
		<div id="warehouse_div1">
			<van-cell-group inset>
				<van-field v-model="material_name" name="材料名称" label="材料名称" :placeholder="material_name" readonly />
				<van-field v-model="material_model" name="规格型号" label="规格型号" :placeholder="material_model" readonly />
			</van-cell-group>
			<div>
				<van-field v-model="equiment_source" readonly label="供应单位" placeholder="选择供应单位" />
			</div>
			<div>
				<van-field v-model="equiment_manu" readonly label="生产厂家" placeholder="选择生产厂家" />
			</div>
			<!-- 原厂配件代码 -->
			<div>
				<van-field v-model="material_code" readonly label="配件代码" placeholder="原厂配件代码" />
			</div>
			<!-- 设备信息 -->
			<div @click="getid()">
				<van-field v-model="equiment_name" is-link readonly label="设备名称" placeholder="选择设备名称" />

			</div>
			<div>
				<van-action-sheet :show="show" :actions="actions" cancel-text="取消" @close="toggleActionSheet"
					@cancel="toggleActionSheet" @select="select_equipment()" />
			</div>
			<!-- 设备管理号码 -->
			<div @click="get_equiment_manage_no()">
				<van-field v-model="equiment_manage_no" is-link readonly label="设备管理号码" placeholder="选择设备管理号码" />
			
			</div>
			<div>
				<van-action-sheet :show="show5" :actions="actions5" cancel-text="取消" @close="toggleActionSheet5"
					@cancel="toggleActionSheet5" @select="select_equipment_manage_no()" />
			</div>
			<!-- 批次库存信息 -->
			<div>
				<van-field v-model="kc_num" readonly label="当前批次库存" placeholder="选择批次信息" />
			
			</div>

			<!-- 材料数量 -->
			<div>
				<van-field required v-model="warehouse_num" label="出库数量" placeholder="出库数量"
					@input="warehouse_num_input" />
			</div>
			
			<!-- 出库日期 -->
			<div>
				<van-field v-model="ware_time" label="出库日期" placeholder="出库日期" @input="ware_time_input" />
			</div>
			<!-- 使用单位 -->
			<div @click="get_company()">
				<van-field v-model="issue_company" is-link readonly label="使用单位" placeholder="选择使用单位" />
			
			</div>
			<div>
				<van-action-sheet :show="show3" :actions="actions3" cancel-text="取消" @close="toggleActionSheet3"
					@cancel="toggleActionSheet3" @select="select_equipment_company()" />
			</div>
			<!-- 使用部位 -->
			<div @click="get_body()">
				<van-field v-model="issue_body" is-link readonly label="使用部位" placeholder="选择使用部位" />
			
			</div>
			<div>
				<van-action-sheet :show="show4" :actions="actions4" cancel-text="取消" @close="toggleActionSheet4"
					@cancel="toggleActionSheet4" @select="select_equipment_body()" />
			</div>
			<!-- 领料人 -->
			<div>
				<van-field v-model="issue_user" label="领料人" placeholder="领料人" @input="issue_user_input" />
			</div>
			<!-- 备注 -->
			<div>
				<van-field v-model="warehouse_remarks" label="出库备注" placeholder="出库备注"
					@input="warehouse_remarks_input" />
			</div>
			<!-- 提交 -->
			<div style="padding-bottom: 5%;padding-top: 5%;">
				<van-button color="#4142F4" type="primary" block @click="materialWarehouse()">出库</van-button>
			</div>
		</div>
	</div>
</template>

<script>
	import {
		BaseUrl
	} from '@/util/util.js';
	const PrefixUrl = BaseUrl();
	var that;
	export default {
		data() {
			return {
				material_id: '',
				material_name: '',
				material_model: '',
				equiment_source: '',
				equiment_manu: '',
				material_code: '',
				option1: [],
				equiment_id: '',
				show: false,
				actions: [],
				equiment_name: '',
				equiment_id: '',
				ware_time: '',
				ware_place: '',
				show1: false,
				actions1: [],
				warehouse_remarks: '',
				uuid: '',
				actions2: [],
				show2: false,
				equiment_no: '',
				equiment_no_id: '',
				issue_user: '',
				issue_body: '',
				issue_company: '',
				show3: false,
				show4: false,
				show5: false,
				actions3: [],
				actions4: [],
				actions5: [],
				equiment_manage_no: '',
				kc_num: ''
			}
		},
		onLoad: function(option) {
			uni.request({
				method: 'POST',
				url: PrefixUrl + '/equimentWare/getWareIssue',
				data: {
					ware_id: option.id
				},
				header: {
					'content-type': 'application/x-www-form-urlencoded',
					'token': uni.getStorageSync('token')
				},
				success: (res) => {
					this.equiment_no_id = res.data.obj.id;
					this.material_name = res.data.obj.materialName;
					this.material_model = res.data.obj.materialModel;
					this.equiment_source = res.data.obj.equimentSource;
					this.equiment_manu = res.data.obj.equimentManu;
					this.material_code = res.data.obj.materialCode;
					this.kc_num = res.data.obj.stockNum;
				}
			});
			
		},
		created() {
			this.getEquiment()
			this.getEquimentPlace()
			this.ware_time = this.format(new Date)
			this.getEquimentCompany()
			this.getEquimentBody()
		},
		methods: {
			getEquiment() {
				uni.request({
					method: 'GET',
					url: PrefixUrl + '/equimentBasis/getAllEquiment',
					data: {},
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						'token': uni.getStorageSync('token')
					},
					success: (res) => {
						for (let i = 0; i < res.data.obj.length; i++) {
							let arr = {
								text: '',
								value: ''
							};
							arr.value = res.data.obj[i].id
							arr.name = res.data.obj[i].equipmentName + '--' + res.data.obj[i].equipmentModel
							this.actions.push(arr)
						}
					}
				});
			},
			getEquimentPlace() {
				uni.request({
					method: 'POST',
					url: PrefixUrl + '/equimentPlace/getAllPlace',
					data: {
						bdst: uni.getStorageSync('userbdst'),
						type: 0
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						'token': uni.getStorageSync('token')
					},
					success: (res) => {
						for (let i = 0; i < res.data.obj.length; i++) {
							let arr = {
								text: '',
								value: ''
							};
							arr.value = res.data.obj[i].id
							arr.name = res.data.obj[i].warePlace
							this.actions1.push(arr)
						}
					}
				});
			},
			getEquimentBody() {
				uni.request({
					method: 'POST',
					url: PrefixUrl + '/equimentPlace/getAllPlace',
					data: {
						bdst: uni.getStorageSync('userbdst'),
						type: 2
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						'token': uni.getStorageSync('token')
					},
					success: (res) => {
						for (let i = 0; i < res.data.obj.length; i++) {
							let arr = {
								text: '',
								value: ''
							};
							arr.value = res.data.obj[i].id
							arr.name = res.data.obj[i].warePlace
							this.actions4.push(arr)
						}
					}
				});
			},
			getEquimentCompany() {
				uni.request({
					method: 'POST',
					url: PrefixUrl + '/equimentPlace/getAllPlace',
					data: {
						bdst: uni.getStorageSync('userbdst'),
						type: 1
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						'token': uni.getStorageSync('token')
					},
					success: (res) => {
						for (let i = 0; i < res.data.obj.length; i++) {
							let arr = {
								text: '',
								value: ''
							};
							arr.value = res.data.obj[i].id
							arr.name = res.data.obj[i].warePlace
							this.actions3.push(arr)
						}
					}
				});
			},
			getid() {
				this.show = true
			},
			toggleActionSheet() {
				this.show = !this.show
			},
			// uuid
			guuid() {
				function S4() {
					return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
				}
				return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
			},
			warehouse_time_input(event) {
				this.warehouse_time = event.detail;
			},
			warehouse_num_input(event){
				this.warehouse_num = event.detail
			},
			warehouse_cess_input(event){
				this.warehouse_cess = event.detail
			},
			warehouse_money_input(event){
				this.warehouse_money = event.detail
			},
			ware_time_input(event){
				this.ware_time = event.detail
			},
			issue_user_input(event){
				this.issue_user = event.detail
			},
			pur_time_input(event){
				this.pur_time = event.detail
			},
			dom_time_input(event){
				this.dom_time = event.detail
			},
			warehouse_remarks_input(event){
				this.warehouse_remarks = event.detail
			},
			get_place(){
				this.show1 = true
			},
			toggleActionSheet1(item) {
				this.show1 = !this.show1
			},
			get_no(){
				this.show2 = true
			},
			get_body(){
				this.show4 = true
			},
			get_company(){
				this.show3 = true
			},
			get_equiment_manage_no(){
				this.show5 = true
			},
			toggleActionSheet2() {
				this.show2 = !this.show2
			},
			toggleActionSheet3() {
				this.show3 = !this.show3
			},
			toggleActionSheet4() {
				this.show4 = !this.show4
			},
			toggleActionSheet5() {
				this.show5 = !this.show5
			},

			//格式化时间
			format(dat) {
				//获取年月日，时间
				var year = dat.getFullYear();
				var mon = (dat.getMonth() + 1) < 10 ? "0" + (dat.getMonth() + 1) : dat.getMonth() + 1;
				var data = dat.getDate() < 10 ? "0" + (dat.getDate()) : dat.getDate();
				var hour = dat.getHours() < 10 ? "0" + (dat.getHours()) : dat.getHours();
				var min = dat.getMinutes() < 10 ? "0" + (dat.getMinutes()) : dat.getMinutes();
				var seon = dat.getSeconds() < 10 ? "0" + (dat.getSeconds()) : dat.getSeconds();
				var newDate = year + mon + data
				return newDate;
			},

			getManuName() {
				uni.redirectTo({
					url: '/pages/material/warehouse/warehouse_manu?id=' + this.material_id +
						'&name=' + this.material_name + '&model=' + this.material_model
				});
			},
			select_equipment(event) {
				this.actions2 = []
				this.equiment_name = event.detail.name
				this.equiment_id = event.detail.value
				uni.request({
					method: 'POST',
					url: PrefixUrl + '/equimentPlace/getAllPlace',
					data: {
						bdst: uni.getStorageSync('userbdst'),
						type: 3,
						equiment_id: this.equiment_id
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded',
						'token': uni.getStorageSync('token')
					},
					success: (res) => {
						for (let i = 0; i < res.data.obj.length; i++) {
							let arr = {
								text: '',
								value: ''
							};
							arr.value = res.data.obj[i].id
							arr.name = res.data.obj[i].warePlace
							this.actions5.push(arr)
						}
					}
				});
			},
			// select_equipment_no(event){
			// 	this.equiment_no = event.detail.name
			// 	this.equiment_no_id = event.detail.value
			// 	this.kc_num = event.detail.text
			// },
			select_equipment_company(event){
				this.issue_company = event.detail.name
			},
			select_equipment_manage_no(event){
				this.equiment_manage_no = event.detail.name
			},
			select_equipment_body(event){
				this.issue_body = event.detail.name
			},
			select_equipment_place(event){
				this.ware_place = event.detail.name
			},
			onConfirm(event){
				this.show1 = false
				this.date = event.detail
			},
			materialWarehouse() {
				if (this.warehouse_num === '') {
					uni.showToast({
						title: '信息未填写完整，请检查!',
						mask: true,
						icon: 'none'
					})
				} else if(this.warehouse_num * 1 > this.kc_num * 1){
					uni.showToast({
						title: '出库数量大于当前批次库存数量!',
						mask: true,
						icon: 'none'
					})
					
				}else {
					uni.request({
						method: 'POST',
						url: PrefixUrl + '/equimentIssue/issueEquiment',
						data: {
							up_bdst: uni.getStorageSync('userbdst'),
							up_user: uni.getStorageSync('username'),
							ware_id: this.equiment_no_id,
							manage_no: this.equiment_manage_no,
							issue_time: this.ware_time,
							issue_company: this.issue_company,
							issue_body: this.issue_body,
							issue_user: this.issue_user,
							remarks: this.warehouse_remarks,
							issue_num: this.warehouse_num
						},
						header: {
							'content-type': 'application/x-www-form-urlencoded',
							'token': uni.getStorageSync('token')
						},
						success: (res) => {
							if(res.data.obj){
								uni.navigateBack({
									delta: 1
								});
								uni.showToast({
									title: '出库成功!',
									mask: true,
									icon: 'none'
								})
							}else {
								uni.showToast({
									title: '出库失败，请联系科信部!',
									mask: true,
									icon: 'none'
								})
							}
						}
					});
				}

			}
		}
	}
</script>

<style>
	#warehouse_div1 {
		position: absolute;
		top: 8vh;
		width: 92%;
		height: 100vh;
		background-color: #FFFFFF;
		left: 4%;
		border-radius: 20rpx;
		padding-top: 5%;
	}

	.warehouse_div1_font {
		margin-left: 5%;
		font-size: 30rpx;
		margin-bottom: 5%;
	}

	.opinion_div3 {
		margin-left: 24%;
		width: 100%;
		display: flex;
	}

	.opinion_div1 {
		margin-left: 4.5%;
		width: 100%;
		display: flex;
	}
</style>
